package com.infolab.aims.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.infolab.aims.domain.Department;
import com.infolab.aims.domain.Section;
import com.infolab.aims.domain.SystemCodeValue;
import com.infolab.aims.domain.SystemRole;
import com.infolab.aims.service.intf.CommonService;


@Service
@Transactional
public class CommonServiceImpl implements CommonService {
	
	
	Log logger = LogFactory.getLog(getClass());
	@PersistenceContext
	private EntityManager em;
	

	public List<Department> getDepartment() {
		List tmp = em.createQuery("from Department where active=1").getResultList();
		
		return tmp;
	}


	public List<Section> getSection(Integer departmentId) {
		Query query = em.createQuery("from Section section where section.department.departmentId=:departmentId order by section.sectionName");
		query.setParameter("departmentId", departmentId);
		return query.getResultList();
	}

	public List<Section> getSection() {
		List tmp = em.createQuery("from Section section where active=1 order by section.sectionName").getResultList();
		
		return tmp;
	}


	public List<SystemRole> getRole(Integer sectionId) {
		Query query = em.createQuery("from SystemRole systemRole where systemRole.section.sectionId=:sectionId");
		query.setParameter("sectionId", sectionId);
		return query.getResultList();
	}
	


	public List<SystemRole> getRole() {
		List tmp = em.createQuery("from SystemRole systemRole where active=1  order by systemRole.roleName").getResultList();
		
		return tmp;
	}


	public List<SystemRole> getSystemAccessRight() {
		List tmp = em.createQuery("from SystemAccessRight systemAccessRight order by accessRightName").getResultList();
		
		return tmp;
	}


	public List<SystemCodeValue> getSystemCodeValue(String systemCodeTypeCode) {
		Query query = em.createQuery("select systemCodeValue from SystemCodeType systemCodeType join systemCodeType.systemCodeValues systemCodeValue where systemCodeTypeCode=:systemCodeTypeCode ORDER BY systemCodeValue.systemCodeValueDescription");
		query.setParameter("systemCodeTypeCode", systemCodeTypeCode);
		return query.getResultList();
	}
	
	public List<SystemCodeValue> getSystemCodeValue(String systemCodeTypeId,Integer accessDepartmentId) {
		Query query = em.createQuery("from SystemCodeValue systemCodeValue where systemCodeType.systemCodeTypeId=:systemCodeTypeId and accessDepartment.departmentId=:accessDepartmentId  ORDER BY systemCodeValue.systemCodeValueDescription");
		query.setParameter("systemCodeTypeId", systemCodeTypeId);
		query.setParameter("accessDepartmentId", accessDepartmentId);
		return query.getResultList();
	}
	
}
